package com.zghhost.stockweb.commandline;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zghhost.stockweb.constans.EastmoneyConstans;
import com.zghhost.stockweb.pojo.GeGuYingyebuEntity;
import com.zghhost.stockweb.pojo.SNameEntity;
import com.zghhost.stockweb.pojo.YingyebuEntity;
import com.zghhost.stockweb.util.HttpUtil;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

/**
 * @author guohua.zhang@zhaopin.com.cn
 * @Date 2019/5/17 05:43
 */
public class DownloadYingyebuData {
    public static Map<String,JSONArray> yingyebuCache = new HashMap<>();
    public static Map<String,JSONArray> geguCache = new HashMap<>();

    public static void main(String[] args)throws Exception{
        String sday = "2019-05-21",eday = "2019-05-21";

        JSONArray jsondata = fetchYingyebuData(sday,eday,1);

        List<YingyebuEntity> data = JSONArray.parseArray(jsondata.toString(),YingyebuEntity.class);

        Map<String,SNameEntity> sNameMap = new HashMap<>();

        for(YingyebuEntity yingyebuEntity : data){
            if(yingyebuEntity == null){
                continue;
            }
            List<SNameEntity> sNameEntities = yingyebuEntity.getSNameArr();
            if(sNameEntities == null){
                continue;
            }
            for(SNameEntity sNameEntity : sNameEntities){
                sNameMap.put(sNameEntity.getSCode(),sNameEntity);
            }
        }

        File dfile = new File("/Users/wow/Downloads/"+sday+"_"+eday+".xls");
        WritableWorkbook wb = Workbook.createWorkbook(new FileOutputStream(dfile));
        WritableSheet ws = wb.createSheet("sheet",0);

        int row=0,col=0;

        ws.addCell(new Label(col++,row,"代码"));
        ws.addCell(new Label(col++,row,"名称"));
        ws.addCell(new Label(col++,row,"操作方"));

        ws.addCell(new Label(col++,row,"买次"));
        ws.addCell(new Label(col++,row,"卖次"));
        ws.addCell(new Label(col++,row,"买额"));
        ws.addCell(new Label(col++,row,"卖额"));
        ws.addCell(new Label(col++,row,"买余"));

        col=0;
        row++;

        for(String key : sNameMap.keySet()){
            SNameEntity sNameEntity = sNameMap.get(key);

            String code = key.split("\\.")[0];
            JSONArray geguJsonData = fetchGeguYingyebu(sday,eday,1,code);
            List<GeGuYingyebuEntity> gegudata = JSONArray.parseArray(geguJsonData.toString(),GeGuYingyebuEntity.class);

            for(GeGuYingyebuEntity gegu : gegudata){
                ws.addCell(new Label(col++,row,code));
                ws.addCell(new Label(col++,row,sNameEntity.getCodeName()));
                ws.addCell(new Label(col++,row,""+gegu.getSalesName()));

                ws.addCell(new Label(col++,row,""+gegu.getBCount()));
                ws.addCell(new Label(col++,row,""+gegu.getSCount()));
                ws.addCell(new Label(col++,row,""+gegu.getSumBMoney()));
                ws.addCell(new Label(col++,row,""+gegu.getSumSMoney()));
                ws.addCell(new Label(col++,row,""+gegu.getSumPMoney()));

                col=0;
                row++;
            }

            Thread.sleep(1321);
        }

        wb.write();
        wb.close();
    }

    public static JSONArray fetchYingyebuData(String startDate,String endDate,int pageNo)throws Exception{
        String cacheKey = startDate+""+endDate;
        JSONArray cache = yingyebuCache.get(cacheKey);

        if(cache == null){
            cache = new JSONArray();
            yingyebuCache.put(cacheKey,cache);
        }

        String resstr = HttpUtil.doHttpGet(String.format(EastmoneyConstans.YINGYEBU_DONGTAI,pageNo,startDate,endDate),"gb2312");
        resstr = resstr.substring(resstr.indexOf("{"));

        JSONObject resjob = JSONObject.parseObject(resstr);

        boolean successed = resjob.getBoolean("success");
        int totalPageSize = resjob.getInteger("pages");
        JSONArray data = resjob.getJSONArray("data");

        System.out.println(resjob.toString());

        cache.addAll(data);

        if(pageNo < totalPageSize){
            Thread.sleep(1892);
            fetchYingyebuData(startDate,endDate,++pageNo);
        }

        return cache;
    }

    public static JSONArray fetchGeguYingyebu(String startDate,String endDate,int pageNo,String code)throws Exception{
        String cacheKey = code;
        JSONArray cache = geguCache.get(cacheKey);

        if(cache == null){
            cache = new JSONArray();
            geguCache.put(cacheKey,cache);
        }

        String resstr = HttpUtil.doHttpGet(String.format(EastmoneyConstans.GEGU_YINGYEBU,pageNo,startDate,endDate,code),"gb2312");

        resstr = resstr.substring(resstr.indexOf("{"));

        JSONObject resjob = JSONObject.parseObject(resstr);

        boolean successed = resjob.getBoolean("success");
        int totalPageSize = resjob.getInteger("pages");
        JSONArray data = resjob.getJSONArray("data");

        System.out.println(resjob.toString());

        cache.addAll(data);

        if(pageNo < totalPageSize){
            Thread.sleep(1892);
            fetchGeguYingyebu(startDate,endDate,++pageNo,code);
        }

        return cache;
    }
}
